package com.ithei.date.异常;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:  ");
        String name = sc.nextLine();
        Student stu = new Student();
        stu.setName(name);
        while (true) {
            try {
                System.out.println("请输入年龄:   ");
                String sage = sc.nextLine();
                int age = Integer.parseInt(sage);
                stu.setAge(sage);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入一个整数...");
                //continue;
            } catch (RuntimeException e) {
                System.out.println("请输入一个符合范围的年龄...........");
                e.printStackTrace();
                //continue;
            }

        }


    }
}

class Student {
    private String name;
    private String age;

    public Student() {
    }

    public Student(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        if (Integer.parseInt(age) >= 18 && Integer.parseInt(age) <= 25) {
            this.age = age;
        } else {
            //throw new RuntimeException("年龄不在范围...");
            throw  new AgeOutOfBoundsException("年龄不在范围");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

class AgeOutOfBoundsException extends RuntimeException {

    public AgeOutOfBoundsException() {
    }

    public AgeOutOfBoundsException(String message) {
        super(message);
    }
}